package controller

import (
	"time"

	"fiber-demo/internal/common"
	"fiber-demo/internal/global"
	"github.com/gofiber/fiber/v3"
	"github.com/golang-jwt/jwt/v4"
)

type AuthController struct{}

var AuthControllerInstance = AuthController{}

func (c *AuthController) Login(ctx fiber.Ctx) error {
	// add login logic code here

	token := jwt.New(jwt.SigningMethodHS256)
	claims := token.Claims.(jwt.MapClaims)

	// fill claims from user data
	claims["exp"] = time.Now().Add(time.Hour * 72).Unix()

	t, err := token.SignedString([]byte(global.Config.Jwt.Secret))
	if err != nil {
		return ctx.JSON(common.NewResponseBuilder().ErrorMessage(err).Build())
	}
	return ctx.JSON(common.NewResponseBuilder().Data(fiber.Map{"token": t}).Build())
}
